<template>
  <view>
    <!-- 搜索 -->
    <UgoSearch></UgoSearch>
    <!-- tab栏 -->
    <u-tabs
      :list="list"
      :is-scroll="false"
      :current="current"
      @change="change"
      active-color="#eb4450"
      bar-width="150"
    ></u-tabs>
    <view v-if="current === 0" class="goods_list">
      <navigator
        v-for="item in goods"
        :key="item.goods_id"
        :url="`/pages/goodsdetail/goodsdetail?goods_id=${item.goods_id}`"
      >
        <view class="goods-img-wrap">
          <u-image
            height="180"
            width="180"
            :src="
              item.goods_small_logo ||
              'https://img0.baidu.com/it/u=3608654078,1915827767&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=888'
            "
          ></u-image>
        </view>
        <view class="goods-info-wrap">
          <view class="goods_name u-line-2">
            {{ item.goods_name }}
          </view>
          <view class="goods_price"> ￥{{ item.goods_price }} </view>
        </view>
      </navigator>
    </view>
    <view v-if="current === 1">销量</view>
    <view v-if="current === 2">销量</view>
  </view>
</template>

<script>
// query
// cid
// pagenum
// pagesize
export default {
  data() {
    return {
      list: [
        {
          name: "综合",
        },
        {
          name: "销量",
        },
        {
          name: "价格",
        },
      ],
      current: 0,
      goods: [],
    };
  },
  async onPullDownRefresh() {
    // 触发下拉刷新时执行
    // 清空数组
    this.goods = [];
    // 还原第一页
    this.params.pagenum = 1;
    // 重新调用请求
    await this.getGoodsList();
    // 利用api关闭下拉刷新
    uni.stopPullDownRefresh();
  },
  onReachBottom() {
    // 页面触底时执行
    if (this.goods.length < this.total) {
      // 页面页数++
      this.params.pagenum++;
      // 重新调用请求
      this.getGoodsList();
      //
    } else {
      uni.showToast({
        title: "没有数据了",
        icon: "none",
      });
    }
  },
  onLoad(options) {
    console.log(options);
    this.params = {
      query: "",
      cid: options.cid,
      // cid: 5,
      pagenum: 1,
      pagesize: 10,
    };
    this.getGoodsList();
  },
  methods: {
    change(index) {
      this.current = index;
    },
    // 获取商品详情列表
    async getGoodsList() {
      const result = await this.$u.get("/goods/search", this.params);
      console.log(result);
      // 数组合并
      this.goods = [...this.goods, ...result.message.goods];
      // 全局变量-总条数
      this.total = result.message.total;
    },
  },
};
</script>

<style lang='scss'>
.goods_list {
  navigator {
    border: 1rpx solid #f0f0f0;
    display: flex;
    .goods-img-wrap {
      flex: 1;
      padding: 20rpx;
    }
    .goods-info-wrap {
      flex: 3;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      .goods_name {
      }
      .goods_price {
        color: red;
      }
    }
  }
}
</style>